
class Foo:
    def __init__(self, n, a):
        self.name = n
        self.age = a

    def show(self):
        print(self.name, self.age)

# obj = Foo() # obj对象，obj也成为Foo类的 实例 （实例化）
# obj1 = Foo()
# obj2 = Foo()
# obj3 = Foo()

# 单例，用于使用同一份实例（对象）
# v = None
#
# while True:
#     if v:
#         v.show()
#     else:
#         v = Foo('abc', 18)
#         v.show()
class Foo:
    __v = None

    @classmethod
    def get_instance(cls):
        if cls.__v:
            return cls.__v
        else:
            cls.__v = Foo()
            return cls.__v

# 不要在使用 类()
obj1 = Foo.get_instance()
print(obj1)
obj2 = Foo.get_instance()
print(obj2)
obj3 = Foo.get_instance()
print(obj3)